package com.ssm.controller;

import com.alibaba.fastjson.JSONArray;
import com.ssm.pojo.Provider;
import com.ssm.pojo.Role;
import com.ssm.service.provider.ProviderService;
import com.ssm.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/provider")
public class ProviderController {
    @Autowired
    @Qualifier("providerServiceImpl")
    private ProviderService providerService;
    @RequestMapping("/list")
    public String toProviderList(String proCode, String proName, Integer pageIndex, HttpServletRequest request, Model model){
        PageSupport ps = new PageSupport();
        Integer providerCount = providerService.getProviderCount(proCode, proName);
        ps.setTotalCount(providerCount);/*设置用户总数*/
        ps.setPageSize(5);/*设置页面大小*/
        if (pageIndex!=null){
            ps.setCurrentPageNo(pageIndex);
        }
        int offset = ps.getOffset();
        List<Provider> providerList = providerService.getProviderList(proCode, proName,offset,ps.getPageSize());
        model.addAttribute("proCode",proCode);
        model.addAttribute("proName",proName);
        model.addAttribute("totalCount",providerCount);
        model.addAttribute("currentPageNo",ps.getCurrentPageNo());
        model.addAttribute("totalPageCount",ps.getTotalPageCount());
        model.addAttribute("providerList",providerList);
        return "providerlist";
    }
    @RequestMapping("/toAddProvider")
    public String toAddProvider(){
        return "provideradd";
    }
    @RequestMapping("/addProvider")
    public void adProvider(Provider provider,HttpServletResponse response) throws IOException {
        int rowCount = providerService.addProvider(provider);
        if (rowCount>0){
            System.out.println("新增供应商成功!");
        }else{
            System.out.println("新增供应商失败!");
        }
        response.sendRedirect("/provider/list");
    }
    @RequestMapping("/isExist")
    @ResponseBody
    public String isExist(String proCode){
        Provider providerByProCode = providerService.getProviderByProCode(proCode);
        Map map = new HashMap<>();
        if (providerByProCode!=null){
            map.put("result","exist");
        }else{
            map.put("result","not exist");
        }
        String data = JSONArray.toJSONString(map);
        return data;
    }
    @RequestMapping("/toModifyProvider")
    public String toModifyProvider(@RequestParam("proid") Integer id,Model model){
        Provider provider = providerService.getProviderById(id);
        model.addAttribute("provider",provider);
        return "providermodify";
    }
    @RequestMapping("/modifyProvider")
    public void modifyProvider(Provider provider,HttpServletResponse response) throws IOException {
        int rowCount = providerService.modifyProvider(provider);
        if (rowCount>0){
            System.out.println("修改供应商成功");
        }else {
            System.out.println("修改供应商失败");
        }
        response.sendRedirect("/provider/list");
    }
    @RequestMapping("/providerView")
    public String providerView(@RequestParam("proid") Integer id,Model model){
        Provider provider = providerService.getProviderById(id);
        model.addAttribute("provider",provider);
        return "providerview";
    }
    @RequestMapping("/deleteProvider")
    @ResponseBody
    public String deleteProvider(@RequestParam("proid") Integer id) throws ServletException, IOException, SQLException {
        Provider provider = providerService.getProviderById(id);
        Map map = new HashMap();
        if (provider==null){
            map.put("result", "not exist");
        }else{
            int rowCount = providerService.deleteProvider(id);
            if (rowCount>0){
                map.put("result", "true");
                System.out.println("删除供应商成功!");
            }else{
                map.put("result", "false");
                System.out.println("删除供应商失败!");
            }
        }
        String data = JSONArray.toJSONString(map);
        return data;
    }

    @RequestMapping("/list.json")
    public void providerList(HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        List<Provider> providerList = providerService.getAllProvider();
        String data = JSONArray.toJSONString(providerList);
        PrintWriter writer = response.getWriter();
        response.setContentType("application/json");
        writer.write(data);
        writer.flush();
        writer.close();
    }
}
